package net.ftzcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import net.ftzcode.model.Account;
import net.ftzcode.model.Product;
import net.ftzcode.util.CuteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {

	public static Logger logger= LoggerFactory.getLogger(App.class);

	public static void main(String[] args) throws InterruptedException {

		String path=System.getProperty("user.dir");
		System.out.println("当前路径："+path);

		logger.info("程序已经启动，开始执行任务...当前执行路径："+path);

		System.out.println("程序启动中..., 当前程序执行路径：" +path);

		List<Account> accountList = new ArrayList<>();
		List<Product> productList = new ArrayList<>();
		CuteUtils.parserConf(accountList, productList);
		System.out.println("读取配置文件信息中..." );
		accountList.stream().forEach(it -> {
			System.out.println("账号信息===》" + it.toString());
		});

		productList.stream().forEach(it -> {
			System.out.println("商品信息===》" + it.toString());
		});

		for (int i = 0;; i++) {
			// System.setProperty("webdriver.firefox.bin", "c:/Program
			// Files(x86)/MozillaFirefox/firefox.exe");//firefox路径 默认路径可以不需要
			WebDriver firefox = new FirefoxDriver(); // 加载驱动
			firefox.get("http://cn.ssgdfm.com/shop/main");// 打开Firefox
			firefox.manage().window().maximize();// 把窗口最大化
			firefox.findElements(By.cssSelector("#top-nav li a")).get(0).click();//点击登录按钮
			Thread.sleep(3000);
			Set<String> winHandels = firefox.getWindowHandles(); // 得到当前窗口的set集合
			List<String> it = new ArrayList<String>(winHandels); // 将set集合存入list对象
			firefox.switchTo().window(it.get(3));
			Thread.sleep(5000);
			WebElement login = firefox.findElement(By.cssSelector("#login-id"));// 找到登录框
			login.sendKeys(accountList.get(i).getUsername());// 输入用户名
			logger.info("登录账号用户名为："+accountList.get(i).getUsername());
			WebElement pass = firefox.findElement(By.cssSelector("#login-password"));// 找到密码框
			pass.sendKeys(accountList.get(i).getPassword());// 输入密码
			
			firefox.findElement(By.cssSelector(".btn-login")).click();// 点击登录按钮
			Thread.sleep(5000);
			firefox.switchTo().window(it.get(0));

			firefox.get(productList.get(i).getUrl());// 打开商品
			logger.info("商品地址为："+productList.get(i).getUrl());
			
			firefox.findElements(By.cssSelector(".num a")).get(productList.get(i).getCount()-1).click();//改变数量
			logger.info("购买数量为："+productList.get(i).getCount());
			Thread.sleep(500);
			// 选择个数 需要改
			firefox.findElements(By.cssSelector("span a")).get(2).click();// 点击立即购买
			Thread.sleep(2000);
			Actions action = new Actions(firefox); // 声明一个动作
			WebElement xia = firefox.findElement(By.cssSelector(".btn-popup-exitInfo img"));// 找到向下滑动到的元素位置
			action.moveToElement(xia).build().perform();// 执行滑动动作
			Thread.sleep(1000);
			firefox.findElement(By.cssSelector(".btn-popup-exitInfo img")).click();// 点击近期出国信息
			firefox.findElement(By.cssSelector(".btn-red")).click();// 点击选择
			Thread.sleep(1000);
			WebElement jxxia = firefox.findElements(By.cssSelector("dd p input")).get(2);// 找到向下滑动到的元素位置
			action.moveToElement(jxxia).build().perform();// 执行滑动动作
			Thread.sleep(1000);

			firefox.findElements(By.cssSelector("dd p input")).get(2).click();// 点击即时使用
			WebElement jxxxia = firefox.findElement(By.cssSelector("#totalUse"));// 找到向下滑动到的元素位置
			action.moveToElement(jxxxia).build().perform();// 执行滑动动作
			Thread.sleep(1000);
			firefox.findElement(By.cssSelector("#totalUse")).click();// 全部使用
			firefox.findElements(By.cssSelector(".order-btn a img")).get(1).click();// 点击确认订单
			Thread.sleep(10000);// 因为加载新的页面所以等待时间比较长

			WebElement jxxxxia = firefox.findElement(By.cssSelector("#exitDestInfo"));// 找到向下滑动到的元素位置
			action.moveToElement(jxxxxia).build().perform();// 执行滑动动作
			Thread.sleep(1000);
			// agree
			firefox.findElement(By.cssSelector("#exitDestInfo")).click();// 点击确认出国
			firefox.findElement(By.cssSelector("#agree")).click();// 点击我同意

			// 点支付宝 $('.radio_class').get(1).click()
			firefox.findElements(By.cssSelector(".radio_class")).get(1).click();
			firefox.findElement(By.cssSelector("#agree01")).click();// 点我同意

			firefox.findElement(By.cssSelector(".goPayment")).click();// 点付款
			Alert jsAlert = firefox.switchTo().alert();
			jsAlert.accept();// 点击弹出框的确认按钮	
			firefox.close();
			
			firefox.switchTo().window(it.get(1));
			firefox.close();	
			logger.info("url地址为:"+productList.get(i).getUrl()+"商品购买成功");			
		}

	}

}
